#! /usr/bin/python
# -*- coding: cp1250 -*-

import glob, re, string, sys, os
import pepe.p_txt.bigtxt as bt
from pepe.glib import findcoldelimiter as fcd
#import pepe.p_txt.findcdelimiter as fcd

n = '\x0D\x0A'

def cntdel(inpfilemask):
    ''' '''
    for inp_file in glob.glob(inpfilemask):

        cd = ""
        out_data = open('cntdel.data.log', 'ab')
        out_errs = open('cntdel.errs.log', 'ab')
        col_cnt = []

        row_cnt = 0

        inp_data = open(inp_file, 'rb')

        ## find out how many columns we have
        # first row (mostly header) is taken as granted to be correct
        for i in range(1):
            line = inp_data.readline()
            if cd == "": cd = fcd(line)
            cc = len(string.split(line, cd))
            if not cc in col_cnt: col_cnt.append(cc)
        inp_data.close()

        # go into file and write lines to output
        for line in bt.lines(inp_file):
            line = re.sub(n, '', line)
            row_cnt += 1
            cc = len(string.split(line, cd))
            if cc == max(col_cnt):
                out_data.write(line + n)
            else:
                out_errs.write(inp_file+'=InputFile:'+
                               str(row_cnt)+'=RowNo:'+
                               str(cc)+'=ColCnt::'+
                               line + n)
        out_data.close()
        out_errs.close()

HELP = """
Purpose:    Count columns delimiters and redirect to files.
Syntax:     csv_cntdel.py <inpfile(s) mask>
Example:    csv_cntdel.py *.csv

import pepe.p_utils.csv_cntdel as cd
cd.cntdel('*.dat')
"""

if __name__ == "__main__":
    ''' '''
    #print sys.argv
    if len(sys.argv) <> 2:
        print HELP
    else:
        cntdel(sys.argv[1])